//
//  MSRoomManagementController.m
//  觅尚
//
//  Created by No.8 on 2019/3/27.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSFinancialSettlementController.h"
#import "MSRFinancialRoomSettlementCell.h"
#import "MSRoomConsumptionController.h"
#import "MSRoomManageMentModel.h"
#import "MSQueryRelationsController.h"
#import "MSSetRoomManagementController.h"
#import "MSFinancialRoomManagementController.h"

#define pageSize 10
// 注意const的位置
static NSString *const cellId = @"cellId";
static NSString *const headerId = @"headerId";
static NSString *const footerId = @"footerId";

@interface MSFinancialSettlementController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,MSRFinancialRoomSettlementCellDelegate>
{
    CustomNavigationBar *naviagtionBar;
    NSInteger page;
    NSInteger count;
}

/** tableview */
@property (strong,nonatomic)UICollectionView * CollectionView;

/** 头部视图 */
@property(strong,nonatomic)UIView *CollectionHeaderView;

/** 数组*/
@property(nonatomic,strong)NSMutableArray *RoomManagementArray;


/** 房间id*/
@property(nonatomic,strong)NSString *roomid;

/** 房间名称*/
@property(nonatomic,strong)NSString *roomName;


/** 房间消费金额*/
@property(nonatomic,strong)NSString *roomMoney;

/** 房间数量*/
@property(nonatomic,strong)UILabel * totleCountLabel;


@end

@implementation MSFinancialSettlementController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    self.RoomManagementArray = [NSMutableArray new];
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.CollectionView.mj_header = header;
    [self.CollectionView.mj_header beginRefreshing];
}
- (void)MJChiBaoZiHeaderRefresh{
    page = 1;
    [self ReloadData];
}
- (void)MJChiBaoZiFootderRefresh{
    
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self CreatCollectionHeaderView];
    
    
    [self CreateNavigationBar];
    
    [self CreatCollectionView];
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.CollectionView.mj_footer = footder;
}
- (void)ReloadData{
    [self.RoomManagementArray removeAllObjects];
    [MBManager showLoading];
    NSDictionary * params = @{@"busid":[WYCAccountTool unarchivebelongBusid],@"uid":[WYCAccountTool unarchiveuid],@"type":@"4",@"page":@(page),@"pageSize":@(pageSize)};
    //    WYCLog(@"---------%@",params);
    [HTTPRequest POST:MSqueryRoomInformation params:params succeed:^(id responseObject) {
        if (CodeStr) {
            [self.CollectionView.mj_header endRefreshing];
            [MBManager hideAlert];
            count = [responseObject[@"data"][@"count"] integerValue];
             self.totleCountLabel.text = [NSString stringWithFormat:@"房间总量: %@间   开房量:%@间",responseObject[@"data"][@"roomRuningSize"],responseObject[@"data"][@"roomOpenSize"]];
            NSMutableArray *temporaryArray = [NSMutableArray new];
            temporaryArray = [MSRoomManageMentModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"roomRuningList"]];
            self.RoomManagementArray = temporaryArray;
        }else{
            [self.CollectionView.mj_header endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page = [params[@"page"] integerValue];
        }
        [self.CollectionView reloadData];
    } failure:^(NSError *error) {
        [self.CollectionView.mj_header endRefreshing];
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
}
- (void)FootderRefreshReloadData{
    [self.RoomManagementArray removeAllObjects];
    [MBManager showLoading];
    NSDictionary * params = @{@"busid":[WYCAccountTool unarchivebelongBusid],@"uid":[WYCAccountTool unarchiveuid],@"type":@"4",@"page":@(page),@"pageSize":@(pageSize)};
    //    WYCLog(@"---------%@",params);
    [HTTPRequest POST:MSqueryRoomInformation params:params succeed:^(id responseObject) {
        if (CodeStr) {
            [self.CollectionView.mj_footer endRefreshing];
            [MBManager hideAlert];
            self.totleCountLabel.text = [NSString stringWithFormat:@"房间总量: %@间   开房量:%@间",responseObject[@"data"][@"roomRuningSize"],responseObject[@"data"][@"roomOpenSize"]];
            self.RoomManagementArray = [MSRoomManageMentModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"roomRuningList"]];
        }else{
            [self.CollectionView.mj_footer endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page --;
        }
        [self.CollectionView reloadData];
    } failure:^(NSError *error) {
        [self.CollectionView.mj_footer endRefreshing];
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
}
- (void)CreatCollectionHeaderView{
    
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, 420 * px)];
    HeaderView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:HeaderView];
    //    self.CollectionHeaderView = HeaderView;
    
    /** 房间总量*/
    UILabel * totleCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(40 * px, 40 * px, ScreenWidth / 3 * 2, 80 * px)];
    totleCountLabel.textColor = [UIColor whiteColor];
    totleCountLabel.text = @"房间总量:70间     开房量:50间";
    totleCountLabel.textAlignment = NSTextAlignmentLeft;
    totleCountLabel.font = [UIFont systemFontOfSize:SYRealValue(35 / 2)];
    [HeaderView addSubview:totleCountLabel];
    self.totleCountLabel = totleCountLabel;
    
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(totleCountLabel.frame) + 40 * px, ScreenWidth, 1)];
    lineView.backgroundColor = BackViewColor;
    [HeaderView addSubview:lineView];
    
    
    /** 开房标记*/
    UIImageView * GetroomIMG = [[UIImageView alloc] initWithFrame:CGRectMake(30 * px, CGRectGetMaxY(lineView.frame) + 60 * px, 30 * px, 30 * px)];
    GetroomIMG.backgroundColor = WYCRGBColor(122, 106, 224);
    GetroomIMG.layer.cornerRadius = 10 * px;
    [HeaderView addSubview:GetroomIMG];
    
    /** 开房label*/
    UILabel * Getroomlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(GetroomIMG.frame) + 5 * px, GetroomIMG.y - 20 * px, 200 * px, 60 * px)];
    Getroomlabel.textColor = WYCRGBColor(126, 138, 155);
    Getroomlabel.text = @"开房";
    Getroomlabel.font = [UIFont systemFontOfSize:SYRealValue(25 / 2)];
    [HeaderView addSubview:Getroomlabel];
    
    
    
    /** 预约标记*/
    UIImageView * AppointmentIMG = [[UIImageView alloc] initWithFrame:CGRectMake(HeaderView.width / 5, GetroomIMG.y, GetroomIMG.width, GetroomIMG.height)];
    AppointmentIMG.backgroundColor = WYCRGBColor(97, 203, 183);
    AppointmentIMG.layer.cornerRadius = 10 * px;
    [HeaderView addSubview:AppointmentIMG];
    
    /** 预约label*/
    UILabel * Appointmentlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(AppointmentIMG.frame) + 5 * px, Getroomlabel.y, 200 * px, 60 * px)];
    Appointmentlabel.textColor = Getroomlabel.textColor;
    Appointmentlabel.text = @"预约";
    Appointmentlabel.font = [UIFont systemFontOfSize:SYRealValue(25 / 2)];
    [HeaderView addSubview:Appointmentlabel];
    
    
    /** 超消费标记*/
    UIImageView * OverConsumptionIMG = [[UIImageView alloc] initWithFrame:CGRectMake(HeaderView.width / 5 * 2, GetroomIMG.y, GetroomIMG.width, GetroomIMG.height)];
    OverConsumptionIMG.backgroundColor = WYCRGBColor(224, 93, 49);
    OverConsumptionIMG.layer.cornerRadius = 10 * px;
    [HeaderView addSubview:OverConsumptionIMG];
    
    /** 超消费label*/
    UILabel * OverConsumptionlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(OverConsumptionIMG.frame) + 5 * px, Getroomlabel.y, 200 * px, 60 * px)];
    OverConsumptionlabel.textColor = Getroomlabel.textColor;
    OverConsumptionlabel.text = @"超消费";
    OverConsumptionlabel.font = [UIFont systemFontOfSize:SYRealValue(25 / 2)];
    [HeaderView addSubview:OverConsumptionlabel];
    
    
    /** 招待标记*/
    UIImageView * EntertainIMG = [[UIImageView alloc] initWithFrame:CGRectMake(HeaderView.width / 5 * 3, GetroomIMG.y, GetroomIMG.width, GetroomIMG.height)];
    EntertainIMG.backgroundColor = WYCRGBColor(224, 93, 49);
    EntertainIMG.layer.cornerRadius = 10 * px;
    [HeaderView addSubview:EntertainIMG];
    
    /** 招待label*/
    UILabel *Entertainlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(EntertainIMG.frame) + 10 * px, Getroomlabel.y, 200 * px, 60 * px)];
    Entertainlabel.textColor = Getroomlabel.textColor;
    Entertainlabel.text = @"招待";
    Entertainlabel.font = [UIFont systemFontOfSize:SYRealValue(25 / 2)];
    [HeaderView addSubview:Entertainlabel];
    
    
    /** 未使用标记*/
    UIImageView * NotusedIMG = [[UIImageView alloc] initWithFrame:CGRectMake(HeaderView.width / 5 * 4, GetroomIMG.y, GetroomIMG.width, GetroomIMG.height)];
    NotusedIMG.backgroundColor = WYCRGBColor(144, 134, 159);
    NotusedIMG.layer.cornerRadius = 10 * px;
    [HeaderView addSubview:NotusedIMG];
    
    /** 招待label*/
    UILabel * Notusedlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(NotusedIMG.frame) + 5 * px, Getroomlabel.y, 200 * px, 60 * px)];
    Notusedlabel.textColor = Getroomlabel.textColor;
    Notusedlabel.text = @"未使用";
    Notusedlabel.font = [UIFont systemFontOfSize:SYRealValue(25 / 2)];
    [HeaderView addSubview:Notusedlabel];
    
    
    UITextField * seachField = [[UITextField alloc] initWithFrame:CGRectMake(Getroomlabel.x, CGRectGetMaxY(Getroomlabel.frame) + 30 * px, HeaderView.width - Getroomlabel.x * 2, 120 * px)];
    seachField.placeholder = @"搜索房间号";
    seachField.layer.cornerRadius = 10 * px;
    seachField.keyboardType = UIKeyboardTypeNumberPad;
    seachField.backgroundColor = BackViewColor;
    [seachField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [seachField setValue:[UIFont systemFontOfSize:SYRealValue(35 / 2)] forKeyPath:@"_placeholderLabel.font"];
    seachField.textColor = [UIColor whiteColor];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"搜索"]];
    imgView.frame = CGRectMake(20 * px, 30 * px, 60 * px, 60 * px);
    seachField.leftView = imgView;
    seachField.leftViewMode = UITextFieldViewModeAlways;
    [HeaderView addSubview:seachField];
    
}

- (void)CreatCollectionView{
    
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    //
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //设置headerView的尺寸大小
    layout.headerReferenceSize = CGSizeMake(ScreenWidth, self.CollectionHeaderView.height);
    //该方法也可以设置itemSize
    //    layout.itemSize = CGSizeMake(744 * px, 438 * px);
    //2.初始化collectionView
    self.CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, [self navBarBottom] + 440 * px, ScreenWidth, ScreenHeight - [self navBarBottom] - 440 * px) collectionViewLayout:layout];
    
    
    self.CollectionView.backgroundColor = [UIColor clearColor];
    //3.注册collectionViewCell //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [self.CollectionView registerClass:[MSRFinancialRoomSettlementCell class] forCellWithReuseIdentifier:cellId];
    //注册headerView 此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为reusableView
    [self.CollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
    
    self.CollectionView.showsVerticalScrollIndicator = NO;
    //4.设置代理
    self.CollectionView.delegate = self;
    
    self.CollectionView.dataSource = self;
    
    [self.view addSubview:self.CollectionView];
}

#pragma mark collectionView代理方法
//返回section个
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
    
}
//设置每个分区的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.RoomManagementArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    /* 查找可复用的单元格，自定义需要注册 */
    MSRFinancialRoomSettlementCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.SettlementButtonDelegate = self;
    [cell.SettlementButton addTarget:self action:@selector(SettlementButtonClick) forControlEvents:UIControlEventTouchUpInside];
//    [cell.OpenRoomButton addTarget:self action:@selector(OpenRoomButtonClick) forControlEvents:UIControlEventTouchUpInside];
    cell.RoomManageMentModel = self.RoomManagementArray[indexPath.row];
    
    return cell;
    
}
- (void)SettlementButtonClick{
    MSFinancialRoomManagementController *FinancialRoomVC = [[MSFinancialRoomManagementController alloc] init];
    FinancialRoomVC.roomid = self.roomid;
    FinancialRoomVC.roomName = self.roomName;
    FinancialRoomVC.roomMoney = self.roomMoney;
    [self.navigationController pushViewController:FinancialRoomVC animated:NO];
    
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ScreenWidth / 2 - 10 * px, 700 * px);
    
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10 * px;
    
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 20 * px;
    
}

- (void)ClickSettlementButtonRoomUid:(NSString *)UID RoomName:(NSString *)RoomName RoomManey:(NSString *)RoomManey{
    
    self.roomid = UID;
    self.roomName = RoomName;
    self.roomMoney = RoomManey;
}
#pragma mark -UICollectionViewDelegate
//UICollectionView被选中的时候调用
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%zd",indexPath.item);
    
}
// 返回这个UICollectionView是否可以被选择
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
    
}
- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"房间管理";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
